home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-11-08 | 7.6 KB | 262 lines | [TEXT/MPS ] |
- program ChangeTextRes;
- {
- ChangeTextRes.p
- ---------------
- An MPW Tool to delete resource fork of MPW text
- files and rewrite the resource fork to specify
- a desired tab setting, font, and font size.
-
- (c) TML Systems, Inc., 1988
- All rights reserved.
- }
-
- uses MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, PasLibIntf,
-
- CursorCtl, IntEnv; { required for MPW Tools }
-
-
- var ResRefNum: integer; { reference number for resource fork of a given file }
- filename: Str255;
- aStringPtr: StringPtr;
- vRefNum: integer; { reference number for default drive }
- fnderInfo: FInfo; { Finder information for a given file }
- anOSError: OSErr; { result from Mac ROM file I/O calls }
- arg: LongInt; { passed to IEFAccess specifies font and font size }
- result: LongInt; { result from IEFAccess calls }
- i: integer;
-
- gFont: integer; { Font number of specified font as returned by GetFNum }
- gFontSize: longint;
- gTabSize: longint; { tab setting }
- gResDelete: boolean; { delete all of file's resources? }
-
-
- function UpperCase(str: Str255): Str255;
- {
- Convert an alpanumeric string to all uppercase characters.
- }
- var i: integer;
- begin
- for i := 1 to length(str) do
- if (str[i] >= 'a') and (str[i] <= 'z') then
- str[i] := chr(ord(str[i]) - 32);
- UpperCase := str;
- end;
-
-
- procedure SyntaxError(err: integer; msg: Str255);
- {
- Display the appropriate syntax error and then
- exit from the program. Return a status value
- of 1 indicating an early termination of program.
- }
- begin
- case err of
- 1: writeln('# ',msg,' is an invalid option');
- 2: writeln('# missing font');
- 3: writeln('# missing font size');
- 4: writeln('# missing tab setting');
- 5: writeln('# ',msg,' is an invalid font');
- 6: writeln('# ',msg,' is an invalid font size');
- 7: writeln('# ',msg,' is an invalid tab size');
- 8: writeln('# the - character must be accompanied by an option');
- 9: begin
- writeln('# Usage - ChangeTextRes [name…] ');
- writeln(' -f fontname # set font of files to fontname');
- writeln(' -s fontsize # set font size of files to fontsize');
- writeln(' -t tabs # set tab setting to tabs');
- end;
- otherwise writeln('fatal error #',err);
- end;
- IEExit(1); { return error status, 1 = syntax error }
- end;
-
-
- procedure HandleOption(opt: Str255; var argIndex: integer);
- {
- Set the appropriate global flag for each command line
- option encountered on the command line. If an invalid
- option is found, give an error message and exit from the
- program. If the option requires an additional command
- line parameter (e.g. -f Monaco), then retrieve the option(s)
- needed and increment the argIndex counter appropriately.
- }
- var NumString,
- str: Str255;
- begin
- str := UpperCase(opt);
- Delete(str, 1,1); {delete the '-' character}
- if str = 'F' then begin { set font }
- argIndex := argIndex + 1;
- if argIndex < argc then begin
- GetFNum(argv^[argIndex]^, gFont);
- if gFont < 0 then
- SyntaxError(5,argv^[argIndex]^);
- end
- else
- SyntaxError(2,'');
- end
- else if str = 'S' then begin { set font size }
- argIndex := argIndex + 1;
- if argIndex < argc then begin
- StringToNum(argv^[argIndex]^,gFontSize);
- if (gFontSize <= 0) or (gFontSize >= 128) then begin
- NumToString(gFontSize,NumString);
- SyntaxError(6,NumString);
- end;
- end
- else
- SyntaxError(3,'');
- end
- else if str = 'T' then begin { set tab }
- argIndex := argIndex + 1;
- if argIndex < argc then begin
- StringToNum(argv^[argIndex]^,gTabSize);
- if (gTabSize <= 0) or (gTabSize >= 25) then begin
- NumToString(gFontSize,NumString);
- SyntaxError(7,NumString);
- end;
- end
- else
- SyntaxError(4,'');
- end
- else if str = 'D' then { delete file's resources }
- gResDelete := true
- else SyntaxError(1,str);
- end;
-
-
- procedure SkipOption(opt: Str255; var argIndex: integer);
- {
- This routine is called only after the command line parameters
- have already been scanned once using HandleOption. The
- purpose of this routine is to properly increment argIndex
- according to the appropriate command line options.
- }
- var str: Str255;
- begin
- str := UpperCase(opt);
- Delete(str, 1,1); {delete the '-' character}
-
- if str = 'F' then { set font }
- argIndex := argIndex + 1
- else if str = 'S' then { set font size }
- argIndex := argIndex + 1
- else if str = 'T' then { set tab size }
- argIndex := argIndex + 1
- else if str = 'D' then
- { nothing }
- end;
-
-
- procedure ReadCommandLine;
- var argVIndex: integer;
- arg: Str255;
- begin
- if argc = 1 then
- SyntaxError(9,'');
- argVIndex := 1;
- while argVIndex < argc do begin
- arg := argv^[argVIndex]^;
- if length(arg) <> 0 then
- if arg[1] = '-' then
- if length(arg) > 1 then
- HandleOption(arg,argVIndex)
- else
- SyntaxError(8,'');
- argVIndex := argVIndex + 1;
- end; { while }
- end;
-
-
- procedure ReportError(error: integer; filename: Str255);
- {
- Generate the appropriate error message then exit from the
- program. Return a status value indicating early
- termination from the program.
- }
- begin
- if error = 0 then
- exit(ReportError);
-
- write(diagnostic,'ERROR! ');
- case error of
- -35: writeln(diagnostic,filename,' volume does not exist');
- -36: writeln(diagnostic,filename,' IO Error');
- -37: writeln(diagnostic,filename,' is a bad filename or volume name');
- -42: writeln(diagnostic,'Too many files open');
- -43: writeln(diagnostic,filename,' not found');
- -45: writeln(diagnostic,filename,' is locked');
- -46: writeln(diagnostic,filename,' is locked by a software flag');
- -47: writeln(diagnostic,filename,' is busy; one or more files are open');
- -53: writeln(diagnostic,filename,' volume not on-line');
- -54: writeln(diagnostic,filename,' cannot be opened for writing, file is locked');
- -61: writeln(diagnostic,filename,' Read/write permission doesn''t allow writing');
- otherwise
- writeln(diagnostic,'OS error #',error,' has occurred.');
- writeln(diagnostic,' Reference Inside Macintosh pp. III:205-209 for further details');
- end;
- IEExit(2);
- end;
-
-
- begin {main program}
- InitCursorCtl(nil); { make first stmt to avoid heap fragmentation }
- InitFonts; { so we can read in font names }
- SetResLoad(false); { so we read in JUST the font names! }
-
- gResDelete := false; { do not delete file's resources }
- gFont := 4; { set the defalut font to Monaco }
- gFontSize := 9; { set the defalut font size to 9 point }
- gTabSize := 3; { set the defalut tab setting to 3 }
-
- ReadCommandLine;
-
- arg := gFont;
- arg := BSL(arg, 16);
- arg := arg + gFontSize;
-
- anOSError:=GetVol(aStringPtr,vRefNum);
- if anOSError <> 0 then
- ReportError(anOSError,aStringPtr^);
-
- i := 1;
- while i < argc do begin
- RotateCursor(32); { Make cursor rotate each time through loop }
- filename := argv^[i]^;
-
- if length(filename) = 0 then begin
- i := i + 1;
- cycle;
- end;
-
- if filename[1] = '-' then
- SkipOption(filename,i)
- else begin
- anOSError := GetFInfo(filename,vRefNum,fnderInfo);
- if anOSError <> 0 then begin
- ReportError(anOSError,filename);
- cycle;
- end
- else begin
- if (fnderInfo.fdType = 'TEXT') and (fnderInfo.fdCreator = 'MPS ') then begin
- if gResDelete then begin
- anOSError := OpenRF(filename,vRefNum,ResRefNum);
- anOSError := SetEOF(ResRefNum,0);
- anOSError := FSClose(ResRefNum);
- end;
- result := IEFAccess(filename,F_STabInfo,gTabSize);
- result := IEFAccess(filename,F_SFontInfo,arg);
- end
- else
- writeln('WARNING! ',filename,' is not an MPW text file, resources not deleted');
- end;
- end;
- i := i + 1;
- end; { while i < argc }
- writeln;
-
- SetResLoad(true);
- IEExit(0); { Normal status return }
- end. {main program}
-